<?php
namespace app\admin\controller;

use app\common\model\Version as VersionModel;

/**
 * 版本控制器
 * Class Recharge
 * @package app\admin\controller
 */
class Version extends Base
{
    /**
     * [index  版本列表]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function index()
    {
        $param = input('param.');
        $model = new VersionModel;
        $user = $this->user;
        $this->getPageAndSize($param);
        $map = [];
        try {
            $map['status'] = ['in','1,2'];
            $count = $model->getCount($map);
            $allpage = intval(ceil($count / $this->size));//总页面
            $lists = $model->getlist($map, $this->from, $this->size);
        } catch (\Exception $e) {
            return admin_json(-1, '获取失败' . $e->getMessage());
        }
        writelog('查看版本列表', 1, $user);
        return admin_json(1, '获取成功', [
            'count' => $count,//总记录数
            'allpage' => $allpage,
            'lists' => $lists,
        ]);
    }
    /**
     * [add  添加]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function add()
    {
        $param = input('param.');
        $model = new VersionModel;
        $user = $this->user;
        try{
            $model->addData($param);
        }catch(\Exception $e){
            return admin_json(-1, '添加失败'.$e->getMessage());
        }
        writelog('添加版本成功',1,$user);
        return admin_json(1, '添加成功');
    }
    /**
     * [edit  编辑]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function edit()
    {
        $param = input('param.');
        $model = new VersionModel;
        $user = $this->user;
        try{
            $model->editData($param);
        }catch(\Exception $e){
            return admin_json(-1, '编辑失败'.$e->getMessage());
        }
        writelog('编辑版本成功',1,$user);
        return admin_json(1, '编辑成功');
    }
    /**
     * [del  删除]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function del()
    {
        $param = input('param.');
        $model = new VersionModel;
        $user = $this->user;
        try{
            $model->changeStatus(['id'=>$param['id']],-1);
        }catch(\Exception $e){
            return admin_json(-1, '删除失败'.$e->getMessage());
        }
        writelog('删除版本成功',1,$user);
        return admin_json(1, '删除成功');
    }
}